package com.mobvoi.serialport.controller;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.mobvoi.serialport.BuildConfig;
import com.mobvoi.serialport.util.LogUtils;
import com.mobvoi.serialport.util.NetworkUtil;
import com.mobvoi.serialport.util.Parser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import timber.log.Timber;


public class MicArrayController {

    private static MicArrayController INSTANCE;
    private MicCallbackParser parser;

    public static MicArrayController getInstance() {
        if (INSTANCE == null) {
            synchronized (MicArrayController.class) {
                if (INSTANCE == null) {
                    INSTANCE = new MicArrayController();
                }
            }
        }
        return INSTANCE;
    }


    /**
     * 默认的初始化串口方法,波特率:115200,串口地址:/dev/ttyS1
     *
     * @param callback ROS上报内容的回调
     * @param path     要上传到ros的日志目录
     * @throws Exception
     */
    public void init(MicCallbackParser.MicCallback callback, String... path) throws Exception {
        parser = new MicCallbackParser.Builder()
                .baudRate(115200)
                .port("/dev/ttyS1")
                .callback(callback)
                .build();
        startListen();
    }


    /**
     * @param baudRate 波特率
     * @param port     串口地址
     * @param callback ROS上报内容的回调
     * @param path     要上传到ros的日志目录
     * @throws Exception
     */
    public void init(int baudRate, String port, MicCallbackParser.MicCallback callback, String... path) throws Exception {
        parser = new MicCallbackParser.Builder()
                .baudRate(baudRate)
                .port(port)
                .callback(callback)
                .build();
        Log.d("uartservice","s4 uart baudRate: " + baudRate + ",port: " + port+ "parser:"+parser);
        startListen();
    }

    private void startListen() throws Exception {
        Log.d("uartservice","s4 control start listen");
        parser.startListen();
    }

    public void stopListen() {
        Log.d("uartservice","s4 control stop listen");
        if (parser != null) {
            parser.stopListen();
            parser = null;
        }
        INSTANCE = null;
    }


    public void sendCommand(String hexstr) {
        Log.d("uartservice","s4  send:"+hexstr);
        parser.sendCommand(hexstr);

    }


}
